# coding = utf-8

import unittest
import numpy as np

from PyMatterSim.utils.logging import get_logger_handle

from PyMatterSim.reader.dump_reader import DumpReader
from PyMatterSim.reader.reader_utils import DumpFileType
from PyMatterSim.static.topodefect import TopoDefect

logger = get_logger_handle(__name__)

READ_TEST_FILE_PATH = "tests/sample_test_data"


class TestTopoDefect(unittest.TestCase):
    """
    Test class for topological defect characterization
    """

    def setUp(self) -> None:
        super().setUp()
        dumpfile = f"{READ_TEST_FILE_PATH}/center.mode.n1320.atom"
        self.readdump = DumpReader(dumpfile, ndim=2, filetype=DumpFileType.LAMMPS)
        self.readdump.read_onefile()

        self.read_mode = DumpReader(
            dumpfile, ndim=2, filetype=DumpFileType.LAMMPSVECTOR, columnsids=[9, 10]
        )
        self.read_mode.read_onefile()

    def test_topo_defect_2d(self) -> None:
        """
        Test topological defect in 2D
        """
        UV_grid = TopoDefect(self.readdump.snapshots, self.read_mode).topo_defect_2d()

        np.testing.assert_almost_equal(
            np.array(
                [
                    [3.16930285e-05, 2.67123901e-05],
                    [3.68391622e-05, 3.25869468e-05],
                    [5.31950767e-05, 5.22249626e-05],
                    [1.85580063e-05, 8.07852963e-05],
                    [1.04345968e-05, 1.11531037e-04],
                    [5.56951757e-05, 1.23488837e-04],
                    [5.87989388e-05, 1.11382388e-04],
                    [4.42885956e-05, 6.96412882e-05],
                    [5.37068591e-05, 4.71321948e-05],
                    [7.79476187e-05, 1.10053091e-04],
                    [8.61240796e-05, 1.38105476e-04],
                    [9.87241071e-05, 1.33413067e-04],
                    [1.26223932e-04, 1.54253856e-04],
                    [1.72016174e-04, 1.90483137e-04],
                    [2.21282183e-04, 1.68759012e-04],
                    [2.46940761e-04, 1.08432100e-04],
                    [2.58684875e-04, 7.42632818e-05],
                    [2.80504130e-04, 7.87143115e-05],
                    [3.02933435e-04, 1.06803264e-04],
                    [3.02298365e-04, 1.26148076e-04],
                    [3.04896357e-04, 1.29684428e-04],
                    [3.27567812e-04, 1.39224123e-04],
                    [3.43628530e-04, 1.75898101e-04],
                    [3.58510046e-04, 1.80134706e-04],
                    [3.93978523e-04, 1.48517607e-04],
                    [4.19578579e-04, 1.14067031e-04],
                    [4.23413738e-04, 9.25747031e-05],
                    [3.85668083e-04, 8.70168861e-05],
                    [3.56208302e-04, 7.66289299e-05],
                    [3.45883120e-04, 4.99234877e-05],
                    [3.21978800e-04, 3.41261244e-05],
                    [3.23737410e-04, 2.20309151e-05],
                    [3.35577797e-04, 5.20415111e-06],
                    [3.29642267e-04, -1.43892294e-05],
                    [3.11437890e-04, -3.69649523e-05],
                    [2.90640082e-04, -5.49243814e-05],
                    [2.74048932e-04, -6.57932722e-05],
                    [2.61646629e-04, -7.16777440e-05],
                    [2.56216435e-04, -7.61687056e-05],
                    [2.58742535e-04, -9.09971408e-05],
                    [2.64978892e-04, -1.03289318e-04],
                    [2.26609250e-04, -7.91261376e-05],
                    [1.52735689e-04, -5.17411282e-05],
                    [1.38371489e-04, -5.55986084e-05],
                    [1.68983073e-04, -7.79231548e-05],
                    [1.57339876e-04, -8.98891075e-05],
                    [1.28551156e-04, -9.07233508e-05],
                    [9.25502065e-05, -1.22531977e-04],
                    [4.85537705e-05, -1.46655035e-04],
                    [7.81451995e-06, -1.35946754e-04],
                    [-2.61096591e-05, -1.19084462e-04],
                    [-4.38481436e-05, -1.15452056e-04],
                    [-7.67490948e-05, -9.51451187e-05],
                    [-1.50144925e-04, -7.94901278e-05],
                    [-2.10690626e-04, -9.49934030e-05],
                    [-2.16575177e-04, -1.00205230e-04],
                    [-2.17375876e-04, -9.24310451e-05],
                    [-2.35054157e-04, -7.84518384e-05],
                    [-2.39125709e-04, -9.13709051e-05],
                    [-1.88617189e-04, -9.68581573e-05],
                    [-1.72790823e-04, -5.06355112e-05],
                    [-2.21333055e-04, -3.97413071e-05],
                    [-2.73620072e-04, -8.64925949e-05],
                    [-3.05706789e-04, -1.41758197e-04],
                    [-3.09194484e-04, -1.62427042e-04],
                    [-3.30916955e-04, -1.43722833e-04],
                    [-3.70200140e-04, -1.17894115e-04],
                    [-3.63823582e-04, -1.02159557e-04],
                    [-3.68855524e-04, -1.00691096e-04],
                    [-4.05551525e-04, -8.69637114e-05],
                    [-4.06698617e-04, -3.72677212e-05],
                    [-3.82196965e-04, 1.07826477e-06],
                    [-3.63402252e-04, 2.33129857e-07],
                    [-3.40255462e-04, -9.02534813e-06],
                    [-3.27116181e-04, -1.75495333e-05],
                    [-3.48752519e-04, -1.10946064e-05],
                    [-3.73764661e-04, -3.65362011e-06],
                    [-3.72636282e-04, -6.11376110e-06],
                    [-3.55046027e-04, -1.39051225e-05],
                    [-3.08788848e-04, -4.39279486e-05],
                    [-2.77552039e-04, -8.32012554e-05],
                    [-2.90320225e-04, -6.03238179e-05],
                    [-2.96958255e-04, -4.03633146e-05],
                    [-2.92144879e-04, -3.95007949e-05],
                    [-2.89549499e-04, -3.36055069e-05],
                    [-2.78864831e-04, -1.78257583e-05],
                    [-2.57731732e-04, 9.42916710e-06],
                    [-2.48517686e-04, 1.07037320e-05],
                    [-2.56619351e-04, 3.96362828e-06],
                    [-2.37952844e-04, -7.76691359e-06],
                    [-1.57148085e-04, -3.72119762e-05],
                    [-1.03168438e-04, -3.16299583e-05],
                    [-1.27435631e-04, 2.25307962e-05],
                    [-1.47019253e-04, 6.76956372e-05],
                    [-1.30727758e-04, 7.14280799e-05],
                    [-1.01723043e-04, 7.00584166e-05],
                    [-8.00643800e-05, 8.71000480e-05],
                    [-5.36193128e-05, 8.21469378e-05],
                    [1.23708582e-06, 4.73742797e-05],
                    [3.16930285e-05, 2.67123901e-05],
                ]
            ),
            UV_grid[:1][0],
        )

        logger.info(f"Finishing test TopoDefect in 2D using {self.readdump.filename}...")
